﻿#include "CFDPostClipSliceWidgetPlane.h"
#include "ui_CFDPostClipSliceWidgetPlane.h"

#include "GUIFrame/MainWindow.h"
#include "PostGraphAdaptor/PostGraphWidgetPlane.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostClipPlane.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostSlicePlane.h"

namespace GUI {
    CFDPostClipSliceWidgetPlane::CFDPostClipSliceWidgetPlane(ClipSlice widgetType, int currentDataID, int parentDataID, QWidget* parent) :
        CFDPostClipSliceWidgetBase(widgetType, parent), _parentDataID(parentDataID) {
        _currentDataID = currentDataID;

        _ui = new Ui::CFDPostClipSliceWidgetPlane();
        _ui->setupUi(this);

        MainWindow* mainWin = FITKAPP->getGlobalData()->getMainWindowT<MainWindow>();
        if (mainWin == nullptr) {
            return;
        }
        QWidget* currentWidget = (QWidget*)this->getGraphWidget(); //mainWin->getRenderWidget()->getMdiArea()->getCurrentWidget();
        _planeWidget = new Interface::PostGraphWidgetPlane(currentWidget);

        connect(_planeWidget, SIGNAL(sigValueChange(double*, double*)), this, SLOT(slotVTKWidgetValueChange(double*, double*)));
        connect(_ui->lineEdit_OriginX, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_OriginY, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_OriginZ, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_NormalX, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_NormalY, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
        connect(_ui->lineEdit_NormalZ, SIGNAL(dataChanged()), this, SLOT(slotDataChange()));
    }

    CFDPostClipSliceWidgetPlane::~CFDPostClipSliceWidgetPlane() {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }

        if (_planeWidget) {
            delete _planeWidget;
            _planeWidget = nullptr;
        }
    }

    void CFDPostClipSliceWidgetPlane::getDataFromWidget(Interface::FITKAbstractCFDPostData * postData) {
        double origin[3] = { 0, 0, 0 };
        double normal[3] = { 0, 0, 0 };
        origin[0] = _ui->lineEdit_OriginX->value();
        origin[1] = _ui->lineEdit_OriginY->value();
        origin[2] = _ui->lineEdit_OriginZ->value();
        normal[0] = _ui->lineEdit_NormalX->value();
        normal[1] = _ui->lineEdit_NormalY->value();
        normal[2] = _ui->lineEdit_NormalZ->value();

        switch (_widgetType) {
            case GUI::ClipSlice::None:
                break;
            case GUI::ClipSlice::Clip: {
                Interface::FITKCFDPostClipPlane* currentData = dynamic_cast<Interface::FITKCFDPostClipPlane*>(postData);
                if (currentData) {
                    currentData->setValue(origin, normal);
                }
                break;
            }
            case GUI::ClipSlice::Slice: {
                Interface::FITKCFDPostSlicePlane* currentData = dynamic_cast<Interface::FITKCFDPostSlicePlane*>(postData);
                if (currentData) {
                    currentData->setValue(origin, normal);
                }
                break;
            }
        }
    }

    void CFDPostClipSliceWidgetPlane::setDataToWidget(Interface::FITKAbstractCFDPostData* postParentData, Interface::FITKAbstractCFDPostData* postData) {
        //获取边界、中心点、轴向
        double bounds[6] = { 0, 0, 0, 0, 0, 0 };
        double origin[3] = { 0, 0, 0 };
        double normal[3] = { 1, 0, 0 };

        if (postParentData) {
            postParentData->getOrigin(origin);
            postParentData->getBounds(bounds);
        }

        switch (_widgetType) {
            case GUI::ClipSlice::None:
                return;
            case GUI::ClipSlice::Clip: {
                Interface::FITKCFDPostClipPlane* currentData = dynamic_cast<Interface::FITKCFDPostClipPlane*>(postData);
                if (currentData) {
                    currentData->getValue(origin, normal);
                }
                break;
            }
            case GUI::ClipSlice::Slice:
                Interface::FITKCFDPostSlicePlane* currentData = dynamic_cast<Interface::FITKCFDPostSlicePlane*>(postData);
                if (currentData) {
                    currentData->getValue(origin, normal);
                }
                break;
        }

        _ui->lineEdit_OriginX->setText(QString::number(origin[0]));
        _ui->lineEdit_OriginY->setText(QString::number(origin[1]));
        _ui->lineEdit_OriginZ->setText(QString::number(origin[2]));
        _ui->lineEdit_NormalX->setText(QString::number(normal[0]));
        _ui->lineEdit_NormalY->setText(QString::number(normal[1]));
        _ui->lineEdit_NormalZ->setText(QString::number(normal[2]));

        _planeWidget->setBounds(bounds);
        _planeWidget->setValue(origin, normal);
    }

    void CFDPostClipSliceWidgetPlane::showEvent(QShowEvent * event) {
        if (_planeWidget) {
            _planeWidget->setIsShow(true);
            reRender();
        }
        CFDPostClipSliceWidgetBase::showEvent(event);
    }

    void CFDPostClipSliceWidgetPlane::closeEvent(QCloseEvent * event) {
        if (_planeWidget) {
            _planeWidget->setIsShow(false);
            delete _planeWidget;
            _planeWidget = nullptr;
            reRender();
        }
        CFDPostClipSliceWidgetBase::closeEvent(event);
    }

    void CFDPostClipSliceWidgetPlane::hideEvent(QHideEvent * event) {
        if (_planeWidget) {
            _planeWidget->setIsShow(false);
            delete _planeWidget;
            _planeWidget = nullptr;
            reRender();
        }
        CFDPostClipSliceWidgetBase::hideEvent(event);
    }

    void CFDPostClipSliceWidgetPlane::slotVTKWidgetValueChange(double * origin, double * normal) {
        if (_ui == nullptr) {
            return;
        }
        _ui->lineEdit_OriginX->setText(QString::number(origin[0]));
        _ui->lineEdit_OriginY->setText(QString::number(origin[1]));
        _ui->lineEdit_OriginZ->setText(QString::number(origin[2]));
        _ui->lineEdit_NormalX->setText(QString::number(normal[0]));
        _ui->lineEdit_NormalY->setText(QString::number(normal[1]));
        _ui->lineEdit_NormalZ->setText(QString::number(normal[2]));
    }

    void CFDPostClipSliceWidgetPlane::slotDataChange() {
        if (_planeWidget == nullptr) {
            return;
        }
        double origin[3] = { 0, 0, 0 };
        double normal[3] = { 0, 0, 0 };
        origin[0] = _ui->lineEdit_OriginX->value();
        origin[1] = _ui->lineEdit_OriginY->value();
        origin[2] = _ui->lineEdit_OriginZ->value();
        normal[0] = _ui->lineEdit_NormalX->value();
        normal[1] = _ui->lineEdit_NormalY->value();
        normal[2] = _ui->lineEdit_NormalZ->value();
        _planeWidget->setValue(origin, normal);
        reRender();
    }
}

